package com.hiyiin.IntelligentHospital.hosp.service;

import com.alibaba.fastjson.JSONObject;
import com.hiyiin.IntelligentHospital.hosp.repository.DepartmentRepository;
import com.hiyiin.IntelligentHospital.model.hosp.Department;
import com.hiyiin.IntelligentHospital.model.hosp.Hospital;
import com.hiyiin.IntelligentHospital.vo.hosp.DepartmentQueryVo;
import com.hiyiin.IntelligentHospital.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService{
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        //把map集合转换为Department对象
        String mapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(mapString,Department.class);

        //判断是否存在相同的数据
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
        Department departmentExist = departmentRepository.findDepartmentByHoscodeAndDepcode(hoscode,depcode);

        if(departmentExist != null){  //存在，更新
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else {                     //不存在，添加
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(Integer currentPage, Integer pageSize, DepartmentQueryVo departmentQueryVo) {
        //分页:0为第一页
        Pageable pageable = PageRequest.of(currentPage-1, pageSize);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        //创建查询条件
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        //创建实例
        Example<Department> departmentExample = Example.of(department, matcher);
        //执行分页查询
        Page<Department> pages = departmentRepository.findAll(departmentExample, pageable);
        return pages;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        //根据医院编号和科室编号查询
        Department department = departmentRepository.findDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合，用于数据最终封装
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号，查询医院的所有科室信息
        Department department = new Department();
        department.setHoscode(hoscode);
        department.setIsDeleted(0);
        Example<Department> example = Example.of(department);

        //所有科室列表
        List<Department> all = departmentRepository.findAll(example);

        Map<String, List<Department>> departmentMap =
                all.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String,List<Department>> entry : departmentMap.entrySet()){
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> departmentList = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(departmentList.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department dept : departmentList) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(dept.getDepcode());
                departmentVo2.setDepname(dept.getDepname());
                children.add(departmentVo2);
            }
            departmentVo1.setChildren(children);
            result.add(departmentVo1);
        }
        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.findDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.findDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
